/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package manageroute;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
import Database.ConnectDB;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;

/**
 *
 * @author norbert
 */
@WebServlet(name="ManageAJServlet", urlPatterns="/ManageAJServlet")
public class ManageAJServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ManageAJServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ManageAJServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
        CallManagePage(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    @SuppressWarnings("static-access")
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);

        //HttpSession session = request.getSession(true);
        
        PrintWriter out = response.getWriter();

        String command = request.getParameter("command");

        if (command != null)
        {
            if (command.equals("add"))
            {
                String kotaAsal = request.getParameter("kotaasal");
                String kotaTujuan = request.getParameter("kotatujuan");
                String tarif = request.getParameter("tarif");

                String error = DBAj.insertDBAj(kotaAsal, kotaTujuan, tarif);

                if (error != null)
                    request.setAttribute("error", error);
            }
            else if (command.equals("edit"))
            {
                String idTrayek = request.getParameter("idTrayek");
                String kotaAsal = request.getParameter("kotaasal");
                String kotaTujuan = request.getParameter("kotatujuan");
                String tarif = request.getParameter("tarif");
                
                String error = DBAj.updateDBAj(idTrayek, kotaAsal, kotaTujuan, tarif);
                if (error != null)
                    request.setAttribute("error", error);
            }
            else if (command.equals("delete"))
            {
                String idTrayek = request.getParameter("idTrayek");
                String error = DBAj.deleteDBAj(idTrayek);
                
                if (error != null)
                    request.setAttribute("error", error);
            }
        }
        CallManagePage(request, response);
    }



    private void CallManagePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //ChatRoom activeroom = rooms.getRoom(room);
        //if (activeroom == null) activeroom = chatfriend.getRoom(room);
        //if (activeroom != null) request.setAttribute("activeroom", activeroom);
        //request.setAttribute("chatroomlist", rooms);
        //request.setAttribute("chatfriendlist", chatfriend);
        //(response.getWriter()).println(room);
        //if (activeroom != null) (response.getWriter()).println(activeroom.getName());

        //HttpSession session = request.getSession(true);

        ArrayList<DBAj> dbajList = DBAj.getAllRows();
        
        if (dbajList != null)
        {
            request.setAttribute("dbajList", dbajList);
        }

        RequestDispatcher dispatcher =  getServletContext().getRequestDispatcher("/ManageAJ.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
